package sxt.main;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

public class GameFrame extends Frame {
    //实例化背景类
    private GameBackground gameBackground;
    //实例化箱子类
    private GameBox gameBox;
    //实列画小人
    private GamePerson gamePerson;
    //存放图片的图片
    private BufferedImage buffimage=new BufferedImage(800,600,BufferedImage.TYPE_4BYTE_ABGR);
    //构造器对游戏进行初始化
    public GameFrame() {
        setSize(800, 600);
        setVisible(true);
        setTitle("推箱子");
        setLocationRelativeTo(null);
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
        //启动线程
        new run().start();
        //调用实例化类
        Instantiation();
        //键盘监听
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                add(e);
            }
        });
    }
    //实例化类
    public void Instantiation(){
        gameBackground = new GameBackground();
        gameBox = new GameBox();
        gamePerson = new GamePerson();
    }
//创建线程
    class run extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(33);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
boolean at=true;
    /**
     * 所有要绘制的东西都在这里面
     * 重写update方法
     * @param g the specified Graphics window
     */
    @Override
    public void update(Graphics g) {
        //判断
        if (gameBox.checkpoint==1){
        //得到图片
        Graphics graphics= buffimage.getGraphics();
      gameBackground.draw(graphics);
      gameBox.draw(graphics,gamePerson,gameBox.checkpoint);
      gamePerson.draw(graphics,250,250);
      //一次性画出来
        g.drawImage(buffimage,0,0,null);
        } else if (gameBox.checkpoint==2) {
            if(at){
                gamePerson.aa=true;
                at=false;
            }
            Graphics graphics= buffimage.getGraphics();
            gameBackground.draw(graphics);
            gameBox.draw(graphics,gamePerson,gameBox.checkpoint);
            gamePerson.draw(graphics,100,100);
            //一次性画出来
            g.drawImage(buffimage,0,0,null);
           // g.setColor(Color.white);
            //String str="游戏胜利";
           // g.drawString("游戏胜利",50,50);
        }

    }
    public void add(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
               gamePerson.walk(1);
               break;
           case KeyEvent.VK_DOWN:
               gamePerson.walk(2);
               break;
               case KeyEvent.VK_LEFT:
                   gamePerson.walk(3);
                   break;
                   case KeyEvent.VK_RIGHT:
                       gamePerson.walk(4);
                       break;

        }
    }
}
